<?php
//获取通知的数据
$xml = $GLOBALS['HTTP_RAW_POST_DATA'];
if(!xml)
{
	die("xml格式异常");
}
//将xml转换为数组
$arr = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
file_put_contents("a.txt", serialize($arr));
//检测签名(暂时省略)

//支付成功
if($arr['result_code'] ==="SUCCESS")
{
	header("content-type:text/html;charset=utf-8");
	$order_no=$arr['out_trade_no'];//获取订单号
	include 'conn.php';
	include 'func.php';
	$conn=connect2();
	//获取订单状态
	$orderObj=fetchOne("select * from qc_order where order_no='{$order_no}'");
	//获取用户对象
	$userObj=fetchOne("select * from qc_user where id={$orderObj['user_id']}");
	//修改订单状态
	$pay_time=date("Y-m-d H:i:s",time());
	update(array("payment"=>8,"status"=>3,"pay_status"=>1,"pay_time"=>$pay_time),"qc_order","order_no='{$order_no}'");
	//优惠券处理(暂时省略)
	
	//更新货品中的库存信息
	//在订单商品列表中根据订单id查出所有的商品
	$order_goods_list=fetchAll("select * from qc_order_goods where order_id=".$orderObj['id']);
	$goods_ids = array();
	foreach($order_goods_list as $item)
	{
		//订单中每件商品的数量
		$goods_nums = $item['goods_nums'];
		//订单中每件商品的货号
		$product_id = $item['product_id'];
		//修改products表中的商品数量
		update(array("store_nums"=>"store_nums-{$goods_nums}"),"qc_products","id=$product_id");
		$goods_ids[$item['goods_id']] = $item['goods_id'];
	}
	
	//更新商品表里的库存信息
	foreach ($goods_ids as $id)
	{
		$nums = getTotalRows("select sum(store_nums) as store_nums from qc_products where goods_id=".$id);
		if($nums)
		{
			//更改goods表中的商品的数量
			update(array("store_nums"=>$nums),"qc_goods","id=".$id);
		}
	}
	
	//更新优惠券 抢购 团购 等信息（省略）
	//赠送积分(是否赠送积分 待定)
	if($orderObj['point'] > 0)
	{
		//累加用户的积分
		update(array("point"=>"point+{$orderObj['point']}"),"qc_user","id=".$orderObj['user_id']);
		//将操作保存到log日志表中
		//$logs = array('user_id'=>$user_id,'value'=>$value,'point'=>$new_point,'note'=>$note,'create_time'=>date('Y-m-d H:i:s'));
		$new_point=$userObj['point']+$orderObj['point'];
		$note='购买商品，订单：'.$order_no.' 赠送'.$new_point.'积分';
		insert(array("user_id"=>$orderObj['user_id'],"value"=>$new_point,"note"=>$note,"create_time"=>date('Y-m-d H:i:s')), "qc_point_log");
	}
	
	//返利奖金计算
	if ($orderObj['hjcb'])
	{
		//用户彩币的累加
		//$model -> data(array('u_pv'=>"`u_pv`+$hjcb"))->where("id=$user_id")->update();
		update(array('u_pv'=>"u_pv+{$orderObj['hjcb']}"),"qc_user","id=".$userObj['id']);
		$jdrid=$userObj['jdr_id'];
		$tjrid=$userObj['tjr_id'];
		$qy=$userObj['qy'];
		for ($jdr_id=$jdrid,$tqy=$qy; $jdr_id >0 ; $jdr_id=$fjdid,$tqy=$fqy)
		{ 
			//$model = new Model('user');
			//$dsmodel = new Model('dstj');
			if ($tqy=='A') {
				$data = array('pje_a'=>"pje_a+{$hjcb}",'yj_a'=>"yj_a+{$hjcb}");
				$dsdata = array('q_a'=>$hjcb,'user_id'=>$jdr_id,'time'=>date('Y-m-d'));
			}elseif($tqy=='B'){
				$data = array('pje_b'=>"pje_b+{$hjcb}",'yj_b'=>"yj_b+{$hjcb}");
				$dsdata = array('q_b'=>$hjcb,'user_id'=>$jdr_id,'time'=>date('Y-m-d'));
			}
			update($data,"qc_user", "id=".$jdr_id);
			//$model->data($data)->where("id=$jdr_id")->update();
			insert($dsdata, "qc_dstj");
			//$dsmodel->data($dsdata)->insert();
			//$obj=$model->where("id=$jdr_id")->find();
			//根据节点人id查询接点人对象
			$obj=fetchOne("select * from qc_user where id=".$jdr_id);
			$fqy=$obj['qy'];
			$fjdid=$obj['jdr_id'];
		}
		
		//读取奖金参数
		$config=include '../protected/config/system.php';//读取配置文件中的信息
		$jjset=$config['jjset'];
		$cx=$jjset['jjset_cx']/100;
		$ss=$jjset['jjset_ss']/100;
		$jdtc=$jjset['jjset_jdtc']/100;
		$jdcs=$jjset['jjset_jdcs'];
		
		
		//见单奖
		for ($i=1; $i <=$jdcs ; ) 
		{
			$fjdid=$jdrid;
			$jdobj=fetchOne("select * from qc_user where id=".$fjdid);
			$jdjb=$jdobj['group_id'];
			$nojj=$jdobj['nojj'];
			if ($fjdid>0)
			{
				$kjdtc=$hjcb*$jdtc;
				$kcx=$kjdtc*$cx;
				$kss=$kjdtc*$ss;
				$hj=$kjdtc*(1-$cx-$ss);
				if ($hj>0 && $jdjb>1)
				{
					$i=$i+1;
					if ($nojj==0) 
					{
						$mx = array('user_id'=>$fjdid,'fromuser'=>$order_no,'jdtc'=>$kjdtc,'cx'=>$kcx,'ss'=>$kss,'hj'=>$hj,'jjtime'=>date('Y-m-d'));
						insert($mx,"qc_jjmx_all");
						//领导奖
						//self::ldj($fjdid,$kjdtc,$order_no,date('Y-m-d'));
						ldj($fjdid,$kjdtc,$order_no, date('Y-m-d'));
					}
				}

			}else break;
		}
		
	}
	
	//对使用代金券的订单，修改代金券的状态(省略)
	
	//生成收款单
	$receivingData = array(
		'order_id'=>$orderObj['id'],
		'user_id'=>$orderObj['user_id'],
		'amount'=>$orderObj['order_amount'],
		'cb'=>$orderObj['hjcb'],
		'create_time'=>date('Y-m-d H:i:s'),
		'payment_time'=>date('Y-m-d H:i:s'),
		'doc_type'=>0,
		'payment_id'=>2,//支付类型 2表明微信支付
		'pay_status'=>1
	);
	//$model->table("doc_receiving")->data($receivingData)->insert();
	insert($receivingData,"qc_doc_receiving");
	
	//统计会员规定时间内的消费金额,进行会员升级。
	shengji($userObj['id']);
	
	//return $order['id'];
	
	
	
	
	
//	$conn=mysql_connect("localhost","qcjndata","qcjndata");
//	mysql_select_db("qcjndata");
//	mysql_query("set names 'utf8'");
//////	$sql="select * from qc_order where order_no='{$order_no}'";
//////	$result=mysql_query($sql);
//////	$obj=mysql_fetch_array($result);
//	$pay_time=date("Y-m-d H:i:s",time());
//	$sql1="update qc_order set payment=2,status=3,pay_status=1,pay_time='{$pay_time}' where order_no='{$order_no}'";
//	//修改订单状态
//	mysql_query($sql1, $conn);
//	
//	//优惠券处理(暂时省略)
//	
//	//更新货品中的库存信息
	

	echo 'success';
}
else
{
	//支付失败
	echo 'fail';
}



//会员升级
function shengji($user_id)
{
	//统计会员规定时间内的消费金额,进行会员升级。
	$config=include '../protected/config/system.php';//读取配置文件中的信息
	$config_other=$config['other'];
	$grade_days = isset($config_other['other_grade_days'])?intval($config_other['other_grade_days']):365;
	//$time = date("Y-m-d H:i:s",strtotime("-".$grade_days." day"));
	//$obj = $model->table("user")->fields("u_pv,id,group_id")->where("id=".$order['user_id'])->find();
	$obj=fetchOne("select u_pv,id,group_id from qc_user where id=".$user_id);
	if(isset($obj)){
		$amount = $obj['u_pv'];
		$ujb=$obj['group_id'];
		//$grade = $model->table('grade')->where('money <= '.$amount)->order('money desc')->find();
		$grade=fetchOne("select * from qc_grade where money<={$amount} order by money desc");
		if($grade){
			if ($grade['id']>$ujb) {
				//$model->table('user')->data(array('group_id'=>$grade['id']))->where("id=".$order['user_id'])->update();
				update(array("group_id"=>$grade['id']),"qc_user","id=".$user_id);
			}
		}
	}
}

//领导奖
function ldj($user_id,$hjcb,$order_no,$mx_time)
{
	//$config = Config::getInstance();
	//$jjset = $config->get("jjset");
	$config=include '../protected/config/system.php';//读取配置文件中的信息
	$jjset=$config['jjset'];
	$cx=$jjset['jjset_cx']/100;
	$ss=$jjset['jjset_ss']/100;
	//$model = new Model('user');
	//$obj=$model->fields("id,tjr_id")->where("id=$user_id")->find();
	$obj=fetchOne("select id,tjr_id from qc_user where id=".$user_id);
	$tjrid=$obj['tjr_id'];
	if ($tjrid) {
		for ($i=1; $i <=5 ; $i++) { 
			//$model = new Model('user');
			$fjdid=$tjrid;

			if ($fjdid>0) {
				$kldj=$hjcb*$jjset['jjset_ldj'.$i]/100;
				$kcx=$kldj*$cx;
				$kss=$kldj*$ss;
				$hj=$kldj*(1-$cx-$ss);
				//$nojj=$model->fields("nojj,id")->where("id=".$fjdid)->find()['nojj'];
				$no=fetchOne("select nojj,id from qc_user where id=".$fjdid);
				$nojj=$no['nojj'];
				if ($hj>=0.01 && $nojj==0) {
					//资金结算
					$mx = array('user_id'=>$fjdid,'fromuser'=>$order_no,'ldj'=>$kldj,'cx'=>$kcx,'ss'=>$kss,'hj'=>$hj,'jjtime'=>$mx_time);
					//$model->table("jjmx_all")->data($mx)->insert();
					insert($mx,"qc_jjmx_all");
				}

			}else break; 
			//$tjrid=$model->table("user")->where("id=".$fjdid)->find()['tjr_id'];
			$tjrObj=fetchOne("select tjr_id from qc_user whee id=".$fjdid);
			$tjrid=$tjrObj['tjr_id'];
		}
	}
}
?>